package com.ahaoyou.listener;

/**
 * @author LH
 * @create 2022/9/1 16:11
 */
public class EventPublisherProvider {

    private MyEventMulticaster eventMulticaster;

    public EventPublisherProvider() {
        this.eventMulticaster = new MyEventMulticaster();
    }


    private MyEventMulticaster getEventMulticaster() throws IllegalStateException {
        if (this.eventMulticaster == null) {
            throw new IllegalStateException("ApplicationEventMulticaster not initialized - " +
                    "call 'refresh' before multicasting events via the context: " + this);
        }
        return this.eventMulticaster;
    }

    protected void publishEvent(Event event) {
        getEventMulticaster().multicastEvent(event);
    }

    protected void addListener(Listener<?> listener) {
        getEventMulticaster().addListener(listener);
    }

    protected void removeListener(Listener<?> listener) {
        getEventMulticaster().removeListener(listener);
    }

    protected void removeListener() {
        getEventMulticaster().removeAllListener();
    }
}